<!--  -->
<template>
  <div class="navbar">
    <Hamburger :is-active="sidebarStatus" class="hamburger-container" @toggleClick="toggleSideBar" />
    <Breadcrumb id="breadcrumb-container" class="breadcrumb-container" />
    <div class="right-menu">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <div class="avatar">{{ getNameFirst }}</div>
          <el-icon class="avatar-down-icon"><CaretBottom /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <router-link to="/">
              <el-dropdown-item>首页</el-dropdown-item>
            </router-link>
            <el-dropdown-item divided @click="openChangePassword">修改密码</el-dropdown-item>
            <el-dropdown-item divided @click.native="logout">
              <span style="display:block;">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <ChangePassword ref="changePasswordRef" />
  </div>
</template>
<script setup lang="ts">
import { useAppStore } from '@/store/app.js';
import Hamburger from './components/Hamburger.vue';
import Breadcrumb from './components/Breadcrumb.vue';
import { useUserStore } from '@/store/user';
import { CaretBottom } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import ChangePassword from './components/ChangePassword.vue';
const store = useAppStore()
const userStore = useUserStore()
const router = useRouter()

const sidebarStatus = computed(() => store.sidebar.opened)

const getNameFirst = computed(() => {
  if (userStore.userInfo && userStore.userInfo.name) return userStore.userInfo.name[0]
  else 'A'
})

// 切换
const toggleSideBar = () => {
  store.toggleSidebar()
}
// 登出
const logout = () => {
  userStore.logout().then(() => {
    router.push(`/login`)
  })
}
// 打开修改密码
const changePasswordRef = ref<InstanceType<typeof ChangePassword>>()
const openChangePassword = () => {
  changePasswordRef.value?.open()
}
</script>
<style scoped lang="scss">
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  display: flex;
  align-items: center;
  width: 100%;

  .hamburger-container {
    width: 50px;
    line-height: 46px;
    height: 100%;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    padding-right: 30px;
    height: 100%;
    line-height: 50px;
    margin-left: auto;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
          background-color: #868686;
          color: #47ff35;
          font-size: 22px;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        .avatar-down-icon {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }

      }
    }
  }
}
</style>